home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / ohlfind.zip / UTIL.C < prev    next >
C/C++ Source or Header  |  1990-07-02  |  5KB  |  178 lines

  1. /* Misc Utilities needed by Find
  2.    Copyright (C) 1987, 1990 Free Software Foundation, Inc.
  3.  
  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 1, or (at your option)
  7.    any later version.
  8.  
  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU General Public License
  15.    along with this program; if not, write to the Free Software
  16.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. #include <stdio.h>
  19. #include <sys/types.h>
  20. #ifndef USG
  21. #include <strings.h>
  22. #else
  23. #include <string.h>
  24. #define index strchr
  25. #define rindex strrchr
  26. #endif
  27. #include "defs.h"
  28.  
  29. char *find_pred_name ();
  30. boolean pred_and ();
  31.  
  32. /* Return the last component of pathname FNAME. */
  33. char *
  34. basename (fname)
  35.      char *fname;
  36. {
  37.   char *p;
  38.  
  39.   /* For "/", "//", etc., return "/". */
  40.   for (p = fname; *p == '/'; ++p)
  41.     /* Do nothing. */ ;
  42.   if (*p == '\0')
  43.     return p - 1;
  44.   p = rindex (fname, '/');
  45.   return (p == NULL ? fname : p + 1);
  46. }
  47.  
  48. /* Return a pointer to a new predicate structure, which has been
  49.    linked in as the last one in the predicates list.
  50.  
  51.    Set `predicates' to point to the start of the predicates list.
  52.    Set `last_pred' to point to the new last predicate in the list.
  53.    
  54.    Set all cells in the new structure to the default values. */
  55.  
  56. struct pred_struct *
  57. get_new_pred ()
  58. {
  59.   register struct pred_struct *new_pred;
  60.  
  61.   if (predicates == NULL)
  62.     {
  63.       predicates = (struct pred_struct *)
  64.     xmalloc (sizeof (struct pred_struct));
  65.       last_pred = predicates;
  66.     }
  67.   else
  68.     {
  69.       new_pred = (struct pred_struct *) xmalloc (sizeof (struct pred_struct));
  70.       last_pred->pred_next = new_pred;
  71.       last_pred = new_pred;
  72.     }
  73.   last_pred->pred_func = NULL;
  74. #ifdef    DEBUG
  75.   last_pred->p_name = NULL;
  76. #endif    /* DEBUG */
  77.   last_pred->p_type = NO_TYPE;
  78.   last_pred->p_prec = NO_PREC;
  79.   last_pred->side_effects = false;
  80.   last_pred->args.str = NULL;
  81.   last_pred->pred_next = NULL;
  82.   last_pred->pred_left = NULL;
  83.   last_pred->pred_right = NULL;
  84.   return (last_pred);
  85. }
  86.  
  87. /* Return a pointer to a new predicate, with operator check.
  88.    Like get_new_pred, but it checks to make sure that the previous
  89.    predicate is an operator.  If it isn't, the AND operator is inserted. */
  90.  
  91. struct pred_struct *
  92. get_new_pred_chk_op ()
  93. {
  94.   struct pred_struct *new_pred;
  95.  
  96.   if (last_pred)
  97.     switch (last_pred->p_type)
  98.       {
  99.       case NO_TYPE:
  100.     error (1, 0, "oops -- invalid default insertion of and!");
  101.     break;
  102.  
  103.       case VICTIM_TYPE:
  104.       case CLOSE_PAREN:
  105.     new_pred = get_new_pred ();
  106.     new_pred->pred_func = pred_and;
  107. #ifdef    DEBUG
  108.     new_pred->p_name = find_pred_name (pred_and);
  109. #endif    /* DEBUG */
  110.     new_pred->p_type = BI_OP;
  111.     new_pred->p_prec = AND_PREC;
  112.     new_pred->args.str = NULL;
  113.  
  114.       default:
  115.     break;
  116.       }
  117.   return (get_new_pred ());
  118. }
  119.  
  120. /* Add a victim of predicate type PRED_FUNC to the predicate input list.
  121.  
  122.    Return a pointer to the predicate node just inserted.
  123.  
  124.    Fills in the following cells of the new predicate node:
  125.  
  126.    pred_func        PRED_FUNC
  127.    args(.str)        NULL
  128.    p_type        VICTIM_TYPE
  129.    p_prec        NO_PREC
  130.  
  131.    Other cells that need to be filled in are defaulted by
  132.    get_new_pred_chk_op, which is used to insure that the prior node is
  133.    either not there at all (we are the very first node) or is an
  134.    operator. */
  135.  
  136. struct pred_struct *
  137. insert_victim (pred_func)
  138.      boolean (*pred_func) ();
  139. {
  140.   struct pred_struct *new_pred;
  141.  
  142.   new_pred = get_new_pred_chk_op ();
  143.   new_pred->pred_func = pred_func;
  144. #ifdef    DEBUG
  145.   new_pred->p_name = find_pred_name (pred_func);
  146. #endif    /* DEBUG */
  147.   new_pred->args.str = NULL;
  148.   new_pred->p_type = VICTIM_TYPE;
  149.   new_pred->p_prec = NO_PREC;
  150.   return (new_pred);
  151. }
  152.  
  153. /* Allocate N bytes of memory dynamically, with error checking.  */
  154.  
  155. char *
  156. xmalloc (n)
  157.      unsigned n;
  158. {
  159.   char *p;
  160.  
  161.   p = malloc (n);
  162.   if (p == 0)
  163.     error (1, 0, "virtual memory exhausted");
  164.   return p;
  165. }
  166.  
  167. void
  168. usage (msg)
  169.      char *msg;
  170. {
  171.   if (msg)
  172.     fprintf (stderr, "%s: %s\n", program_name, msg);
  173.   fprintf (stderr, "\
  174. Usage: %s path... [expression]\n",
  175.        program_name);
  176.   exit (1);
  177. }
  178.